<!--
 * @Author: your name
 * @Date: 2021-04-10 10:27:43
 * @LastEditTime: 2021-06-04 09:30:08
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ebc-admin\src\views\basics\GeneralSet\Short.vue
-->
<template>
  <div class="no-padding">
    <!-- 列表 -->
    <tab-list
      ref="list"
      :searchs="searchs"
      :list-hedar="listHedar"
      :params="params"
      otherShow
      :api-function="apiFunction"
      @add-list="openEditBox()"
      @init-params="initParams"
    >
      <template slot="operate" slot-scope="scope">
        <el-button type="text" @click="openEditBox(scope.value.id)">编 辑</el-button>
         <el-button type="text" @click="enable(scope.value)">{{scope.value.status == '1' ? '停 用' : '启 用'}}</el-button>
        <el-button type="text" @click="deleteItem(scope.value.id)">删 除</el-button>
      </template>
    </tab-list>
    <!-- 短信模板编辑弹窗 -->
    <maeesge-box ref="box" @submit="submit" title="编辑定时任务">
      <edit-from-list 
        ref="edit" 
        :params="editParams" 
        :editList="editList" 
        @createRule="createRule" />
    </maeesge-box>
    <!-- cron表达式弹窗 -->
    <div class="msg2">
      <maeesge-box ref="box2" title="生成表达式">
        <cronRule ref="cron" />
        <div slot="footer" class="box2-footer">
            <el-button type="primary" size="mini" @click="submit2()">确定</el-button>
            <el-button type="warning" size="mini" @click="reset()">重置</el-button>
            <el-button size="mini" @click="close">取消</el-button>
        </div>
      </maeesge-box>
    </div>
  </div>
</template>

<script>
// 组件
import tabList from '@/components/admin/tab-list.vue'
import maeesgeBox  from '@/components/admin/maeesge-box';
import editFromList from '@/components/admin/edit-from-list.vue';
import cronRule from '@/components/admin/cron-rule';

// // 数据
import { configData, createEditData } from './scheduled.config'

// 接口
import { platformAll, platformDel, platformDetail, platformEdit, platformUse, platformTrigger } from '@/api/operations'
import { tagDownList, tagDownList2 } from '@/api/news.js'
import { asyncApiFun, openMessageBox } from '@/utils/utils'

export default {
  name: 'Short',
  components: {
    tabList,
    maeesgeBox,
    editFromList,
    cronRule
  },
  data() {
    return {
      ...configData(),
      editList: [],
      params: {},
      editParams: null,
      templateList: [],
      tagDownList: [],
      apiFunction: platformAll
    }
  },
  // created() {
  //   this.getTemplateList();
  // },
  methods: {
    submit2() {
      this.editList.forEach(item => {
        if (item.key === 'jobCron') item.value = this.$refs.cron.getLastData();
      })
      this.close();
    },
    reset() {
      this.$refs.cron.reset();
    },
    close() {
      this.reset()
      this.$refs.box2.close();
    },
    async submit() {
      const [res] = await asyncApiFun(platformEdit, this.$refs.edit.ruleFrom());
      if (res) {
        this.$message.success('操作成功')
        this.$refs.box.close();
        this.$refs.list.getList();
      }
    },
    deleteItem(id) {
      openMessageBox({
        content: '删除数据后，将不可恢复，是否确认？',
        title: '删除数据'
      }, async boole => {
        if (boole) {
          const [res] = await asyncApiFun(platformDel, id)
          if (res) this.$refs.list.getList()
        }
      })
    },
    async openEditBox(id) {
      let _detail = null;
      if (id) {
        this.editParams = {id};
        const [res] = await asyncApiFun(platformDetail, id);
        if (res) {
          _detail = res.data;
        }
      }
      const [res2] = await asyncApiFun(tagDownList2);
      const [res3] = await asyncApiFun(platformTrigger);
      this.editList = createEditData(
        _detail, 
        res3.data.map(item => ({value: item.id, title: item.notifyTitle})), 
        res2.data.map(item => ({value: item.tagId, title: item.tagName}))
      );
      this.$refs.box.open();
    },
    createRule() {
      this.$refs.box2.open();
    },
    async enable({status, id}) {
      const [res] = await asyncApiFun(platformUse, {status: status == 1 ? 2 : 1, id});
      if (res) this.$refs.list.getList();
    },
    // 初始化列表请求参数
    initParams(param) {
      this.params = param
    }
  }

}

</script>

<style lang="scss" scoped>
  .box2-footer{
    text-align: center;
    padding-top: 10px;
  }
  .msg2::v-deep .el-dialog{
    max-height: 90%;
    overflow: auto;
    margin-top: 5vh!important;
  }
</style>
